package com.nexgo.payment.trans.preauthcomvoid;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.nexgo.payment.App;
import com.nexgo.payment.R;
import com.nexgo.payment.action.confirmtransdetail.ConfirmTransDetailFragment;
import com.nexgo.payment.action.inputadminpwd.AdminPasswordFragment;
import com.nexgo.payment.action.inputtracenum.InputTraceNumFragment;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.trans.AbsTransActivity;
import com.nexgo.payment.util.ActivityUtils;

/**预授权完成撤销
 * Created by wanghongyang on 2017/8/24.
 */

public class PreAuthComVoidActivity extends AbsTransActivity<PreAuthComVoidContract.Presenter> implements PreAuthComVoidContract.View{
    private static final String TRACE_NUM = "trace_num";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        String traceNum = null;
        if (bundle != null){
            traceNum = bundle.getString(TRACE_NUM, "");
        }
        new PreAuthComVoidPresenter(this, Repository.getInstance(), traceNum);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.start();
    }

    @Override
    public void showInputAdminPassword() {
        AdminPasswordFragment adminPwdFragment = AdminPasswordFragment.newInstance();
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), adminPwdFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), adminPwdFragment, R.id.contentFrame);
        }
    }

    @Override
    public void showInputTraceNum() {
        InputTraceNumFragment traceNumFragment = InputTraceNumFragment.newInstance();
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), traceNumFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), traceNumFragment, R.id.contentFrame);
        }
    }

    @Override
    public void showConfirmTransDetail(String traceNum) {
        ConfirmTransDetailFragment transDetailFragment = ConfirmTransDetailFragment.newInstance(traceNum);
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), transDetailFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), transDetailFragment, R.id.contentFrame);
        }
    }
    @Override
    public void showCardNoConfirm(TransData transData) {
        if(!mPresenter.checkOrigCardNo(transData.getPan())){
            showError(App.sContext.getString(R.string.cardno_not_exsit));
        }else {
            super.showCardNoConfirm(transData);
        }
    }

    public static Intent newInstance(Context context, String traceNum) {
        Intent intent = new Intent(context, PreAuthComVoidActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(TRACE_NUM, traceNum);
        intent.putExtras(bundle);
        return intent;
    }

}
